package org.example.utils;


import jakarta.validation.*;

import java.util.Set;

/**
 * @author: sql
 * @date: 2024/6/7
 * @Description: 校验工具类
 * @version: 1.0
 */
public class ValidationUtil {
    private static Validator validator;

    static {
        ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
        validator = vf.getValidator();
    }


    /**
     * @param t 将要校验的对象
     * @throws ValidationException
     * @throws ValidationException
     * @throws ValidationException void
     * @throws
     * @Description: 校验方法
     */
    public static <T> String validate(T t) throws ValidationException {
        Set<ConstraintViolation<T>> set = validator.validate(t);
        StringBuilder validateError = new StringBuilder();
        if (set.size() > 0) {
            for (ConstraintViolation<T> val : set) {
                validateError.append(val.getMessage() + " ;");
            }
        }
        return validateError.toString();
    }
}
